<template>   <!--资产添加/更新dialog-->
  <el-dialog
    :title="!dataForm.assetsId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-row>
      <el-col :span="12">
        <el-form-item label="资产名称" prop="assetsName">
          <el-input v-model="dataForm.assetsName" placeholder="资产名称"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="编号" prop="assetsNum">
          <el-input v-model="dataForm.assetsNum" placeholder="编号"></el-input>
        </el-form-item>
      </el-col>
      </el-row>

      <el-form-item label="资产图片" prop="assetsImg" v-model="dataForm.assetsImg">
      <el-upload
        class="avatar-uploader"
        :action="url1"
        :show-file-list="false"
        :on-success="successHandle">
        <img v-if="dataForm.assetsImg" :src=dataForm.assetsImg class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      </el-form-item>
      <el-col span="18">
      <el-form-item label="备注" prop="assetsDesc">
        <el-input v-model="dataForm.assetsDesc" placeholder="备注"></el-input>
      </el-form-item>
      </el-col>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      url1: '',
      visible: false,
      dataForm: {
        assetsId: 0,
        assetsName: '',
        assetsBuytime: '',
        assetsNum: '',
        assetsAdmin: '',
        assetsImg: '',
        assetsDesc: '',
        assetsStatus: 0       // 默认状态正常
      },
      dataRule: {
        assetsName: [
          { required: true, message: '资产名称不能为空', trigger: 'blur' }
        ],
       /* assetsBuytime: [
          { required: true, message: '购入日期不能为空', trigger: 'blur' }
        ], */
        assetsNum: [
          { required: true, message: '编号不能为空', trigger: 'blur' }
        ],
     /*   assetsAdmin: [
          { required: true, message: '购入人不能为空', trigger: 'blur' }
        ], */
        assetsImg: [
          { required: true, message: '资产照片不能为空', trigger: 'blur' }
        ],
        /* assetsDesc: [
           { required: true, message: '备注不能为空', trigger: 'blur' }
        ], */
        assetsStatus: [
          { required: true, message: '0：正常，1禁用不能为空', trigger: 'blur' }
        ]
      }
    }
  },

  activated () {
    this.getDataList(); // 获取数据列表
  },
  methods: {
    init (id) {
      this.url1 = this.$http.adornUrl(`/file/upload4?token=${this.$cookie.get('token')}`)
      this.dataForm.assetsId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.assetsId) {
          this.$http({
            url: this.$http.adornUrl(`/assets/info/info/${this.dataForm.assetsId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.assetsName = data.info.assetsName
              this.dataForm.assetsBuytime = data.info.assetsBuytime
              this.dataForm.assetsNum = data.info.assetsNum
              this.dataForm.assetsAdmin = data.info.assetsAdmin
              this.dataForm.assetsImg = data.info.assetsImg
              this.dataForm.assetsDesc = data.info.assetsDesc
              this.dataForm.assetsStatus = data.info.assetsStatus
            }
          })
        }
      })
    },
    // 上传成功
    successHandle (response, file, fileList) {
      console.log(response)
      this.dataForm.assetsImg = response.assetsImg  // 上传成功后，更新图片路径
      this.$message.success('上传成功')
    },
    //  获得当前时间
    getCurrentTime () {
      return new Date()
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/assets/info/${!this.dataForm.assetsId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'assetsId': this.dataForm.assetsId || undefined,
              'assetsName': this.dataForm.assetsName,
              'assetsBuytime': this.getCurrentTime(),
              'assetsNum': this.dataForm.assetsNum,
              'assetsAdmin': this.dataForm.assetsAdmin,
              'assetsImg': this.dataForm.assetsImg,    // 没传回去
              'assetsDesc': this.dataForm.assetsDesc,
              'assetsStatus': this.dataForm.assetsStatus
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
